package nongchang.kuaijiecaifu.com.ncapp_android.ui;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import com.mobsandgeeks.saripaar.annotation.Order;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ezy.lite.util.DateTime;
import ezy.lite.util.UI;
import ezy.widget.view.LabelEditText;
import nongchang.kuaijiecaifu.com.ncapp_android.Const;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.common.activity.BaseActivity;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.event.AddressEvent;
import nongchang.kuaijiecaifu.com.ncapp_android.util.RxBus;
import nongchang.kuaijiecaifu.com.ncapp_android.util.validator.DefaultValidator;
import pickerview.OptionsPickerView;
import pickerview.TimePickerView;

public class UserAddAddressActivity extends BaseActivity {
    @Order(3)
    @NotEmpty(message = "提货时间不能为空")
    @BindView(R.id.txt_time)
    TextView txtTime;
    @BindView(R.id.btn_date)
    ImageView btnDate;
    @BindView(R.id.edit_address)
    TextView txtAddress;
    @Order(1)
    @NotEmpty(message = "姓名不能为空")
    @BindView(R.id.txt_name)
    LabelEditText txtName;
    @Order(2)
    @NotEmpty(message = "手机号不能为空")
    @BindView(R.id.txt_phone)
    LabelEditText txtPhone;
    @BindView(R.id.btn_save)
    TextView btnSave;
    OptionsPickerView pvOptions;
    private ArrayList<String> options1Items = new ArrayList<String>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<ArrayList<String>>();
    DefaultValidator validator;
    Calendar cal;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_add_address);
        ButterKnife.bind(this);
//        UI.setTvHtml(txtAddress, R.string.add_address);
        pvOptions = new OptionsPickerView(this);
        cal = Calendar.getInstance();
        cal.setTime(new Date());
//        86400000
        String week="";
        String date="";
        long time;
        ArrayList<String> options2Items_01 = new ArrayList<String>();
        options2Items_01.add("09:50-15:00");
        options2Items_01.add("15:00-19:00");
        for(int i=0;i<7;i++){
            time=cal.getTimeInMillis()+(long)86400000;
            cal.setTime(new Date(time));
            date= DateTime.toDate(cal.getTime());
            week=DateTime.toWeek(cal.get(Calendar.DAY_OF_WEEK));
            if(i==0) {
                options1Items.add("（明天）"+date + week);
                txtTime.setText("（明天）"+date + week);

            }else{
                options1Items.add(date + week);
            }
            options2Items.add(options2Items_01);
        }



        pvOptions.setPicker(options1Items, options2Items, false);
        pvOptions.setTitle("选择时间");
        pvOptions.setCyclic(false);
        pvOptions.setSelectOptions(1, 1);
        pvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                //返回的分别是三个级别的选中位置
                String tx = options1Items.get(options1) + options2Items.get(options1).get(option2);
                UI.setTvHtml(txtTime,R.string.address_tostring,tx);
            }
        });
        validator = new DefaultValidator(this);
        validator.clicked(btnSave);
        validator.succeeded(()->{
            RxBus.post(new AddressEvent(UI.toString(txtPhone), UI.toString(txtName), UI.toString(txtAddress), UI.toString(txtTime)));
            finish();
        });
    }

    @OnClick({R.id.btn_date, R.id.btn_back})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_date:
                pvOptions.show();
                break;
            case R.id.btn_back:
                finish();
                break;
        }
    }
}
